import axios from 'axios'
import store from '@/store'
import router from '@/router'
import Cookies from 'js-cookie'

// 创建axios实例
const service = axios.create({
  timeout: 60000, // 请求超时时间 60s
  withCredentials: true,
  // headers:{
  //   'Content-Type': 'application/x-www-form-urlencoded'
  // }
})

// request拦截器
service.interceptors.request.use(
  config => {
    config.baseURL = process.env.API_2
    if(config.url.indexOf("/api/user") != -1 ){
      config.baseURL = process.env.API_1
    }
    if(config.url.indexOf("/api/voice") != -1 ){
      config.baseURL = process.env.API_3
    }
    let langType = ((localStorage.getItem('lang') ? localStorage.getItem('lang') : 'zh'))
    config.headers['lang'] =  langType
    store.commit('GET_STATUS')
    if (store.getters.status) {
      config.headers['Authorization'] =  store.getters.status// 让每个请求携带自定义token 请根据实际情况自行修改
    }
    return config
  },
  error => {
    console.log(error) // for debug
    Promise.reject(error)
  }
)

// respone拦截器
service.interceptors.response.use(
  response => {
    // console.log(response)
    if(response.request.responseType == 'blob') {
      return response
    } else {
      // errNo为0时请求成功，非0时抛错
      const res = response.data
      const errNo = res.errorNumber
      const errMsg = res.errorMessage
      if (errNo != 0) {
        if (errNo == 111000) {
          Cookies.remove('U')
          store.commit('REMOVE_STORAGE')
          router.replace({
              path: '/home' // 到登录页重新获取token
          })
        }
        var result = {
          errno: ((errNo) ? errNo : 10500),
          errmsg: ((errMsg) ? errMsg : '服务器异常，请重试')
        }
        return Promise.reject(result)
      } else {
        return response.data
      }
    }
  },
  error => {
    // console.log('err：' + error) // for debug
    var response = error.response
    // console.log(response)
    /*if(response == undefined){
      Cookies.remove('U')
      store.commit('REMOVE_STORAGE')
      router.replace({
          path: '/home' // 到登录页重新获取token
      })
      return
    }*/
    var result = {
      errno: (response) ? response.status : 10500,
      errmsg: '未知错误 ' + ((response) ? response.status : '')
    }
    switch(response.status){
      case 400:
          result.errno = 10400;
          result.errmsg = '参数错误';
          break;
      case 401:
          result.errno = 10401;
          result.errmsg = '账号未授权';
          break;
      case 403:
          result.errno = 10403;
          result.errmsg = '拒绝访问';
          break;
      case 404:
          result.errno = 10404;
          result.errmsg = '接口不存在';
          break;
      case 404:
          result.errno = 10405;
          result.errmsg = '请求方法不允许';
          break;
      case 406:
          result.errno = 10406;
          result.errmsg = '资源不可访问';
          break;
      case 408:
          result.errno = 10408;
          result.errmsg = '请求超时';
          break;
      case 500:
          result.errno = 10500;
          result.errmsg = '服务器异常，请重试';
          break;
    }
    return Promise.reject(result)
  }
)

export default service
